﻿// ============================================================================
//  _ __   ___  ___  ___ _ __ ___  _   _
// | '_ \ / _ \/ __|/ _ \ '_ ` _ \| | | |
// | | | |  __/\__ \  __/ | | | | | |_| |
// |_| |_|\___||___/\___|_| |_| |_|\__,_|
//
// NES Emulator by daxnet, 2024
// MIT License
// ============================================================================

using Microsoft.Xna.Framework;
using MonoGame.Forms.NET.Controls;

namespace NesEmu.Console;

internal class ControllerSettingControl : MonoGameControl
{
    #region Public Events

    public event EventHandler<ControllerKeysPressedEventArgs>? ControllerKeysPressed;

    #endregion Public Events

    #region Public Classes

    public class ControllerKeysPressedEventArgs(string[] pressedKeys) : EventArgs
    {
        #region Public Properties

        public string[] PressedKeys { get; } = pressedKeys;

        #endregion Public Properties
    }

    #endregion Public Classes

    #region Private Fields

    private readonly Input _input = new();
    private TimeSpan _tick = TimeSpan.Zero;

    #endregion Private Fields

    #region Protected Methods

    protected override void Draw()
    {
    }

    protected override void Initialize()
    {
        AlwaysEnableKeyboardInput = true;
    }

    protected override void Update(GameTime gameTime)
    {
        _tick += gameTime.ElapsedGameTime;
        if (_tick > TimeSpan.FromMilliseconds(100))
        {
            _tick = TimeSpan.Zero;
            var pressedKeys = _input.GetPressedKeys();
            if (pressedKeys.Length > 0)
            {
                ControllerKeysPressed?.Invoke(this, new ControllerKeysPressedEventArgs(pressedKeys));
            }
        }
    }

    #endregion Protected Methods
}